﻿var ns = namespace("kaskelot.sp.ui");

ns.todo = function () {

	function initByggnadTable() {
		//kaskelot.sp.ui.sorting.toggleSort("#todo-byggnader .byggnad-table tbody", "td.name", "tr", "asc");
		initSearchFilter("#todo-byggnader input.filter", ".byggnad-table tbody");
		initFilterButton("#todo-byggnader");
	}

	function initPersonTable() {
		//kaskelot.sp.ui.sorting.toggleSort("#todo-personer .person-table tbody", "td.name", "tr", "asc");
		initSearchFilter("#todo-personer input.filter", ".person-table tbody");
		initFilterButton("#todo-personer");
	}

	function initFilterButton(containerSelector) {
		var textField = $(containerSelector + " " + "input[type=text]");
		var btn = $(containerSelector + " " + "input[type=button]");
		btn.click(function () {
			textField[0].value = "";
			$(containerSelector).find("table tr").show();
		});
	}

	function initSearchFilter(selector, contentSelector) {
		$(selector).keyup(function (e) {

			if (e.keyCode === 27)
				this.value = "";

			//hide all the rows
			$(contentSelector).find("tr").hide();
			//split the current value of searchInput
			var data = this.value.split(" ");

			//create a jquery object of the rows
			var jo = $(contentSelector).find("tr");
			//Recursively filter the jquery object to get results. 
			$.each(data, function (i, v) {
				var regEx = new RegExp(v, 'i');
				jo = jo.filter(function () { return $(this).text().match(regEx) });
			});
			//show the rows that match.
			jo.show();
			//Removes the placeholder text 
		}).focus(function () {
			this.value = "";
			$(this).unbind('focus');
		});
	}

	function getPersonList() {
		$(".blockable-container").block({ message: null });

		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientTodoService.svc/GetPersonHandelseLoggTodo",
			type: "GET",
			data: {},
			processData: true,
			cache: false,
			success:
				function (data) {
					if (!data.GetPersonHandelseLoggTodoResult.HasError) {
						$("#todo-personer .person-table tbody").append(
							$("#person-todo-template").render(data.GetPersonHandelseLoggTodoResult.Data)
						);
						initPersonTable();
					}
					$(".blockable-container").unblock();
				},
			error:
				function (xhr) {
					$(".blockable-container").unblock();
					kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
				}
		});
	}

	function getByggnadList() {
		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientTodoService.svc/GetByggnadHandelseLoggTodo",
			type: "GET",
			data: {},
			processData: true,
			cache: false,
			success:
				function (data) {
					if (!data.GetByggnadHandelseLoggTodoResult.HasError) {
						$("#todo-byggnader .byggnad-table tbody").append(
							$("#byggnad-todo-template").render(data.GetByggnadHandelseLoggTodoResult.Data)
						);
						initByggnadTable();
					}
					$(".blockable-container").unblock();
				},
			error:
				function (xhr) {
					$(".blockable-container").unblock();
					kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
				}
		});
	}

	function getBlankettList(blankettTyp, selector) {
		var inkomna = true;
		var pausade = false;
		var godkanda = false;
		var avslagna = false;

		if (blankettTyp === "Ansökan"){
			inkomna = $("#inkomna").is(":checked");
			 pausade = $("#pausade").is(":checked");
			 godkanda = $("#godkanda").is(":checked");
			 avslagna = $("#avslagna").is(":checked");
		}

		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientTodoService.svc/GetBlankettPosterTodo",
			type: "GET",
			data: { blankett: blankettTyp, inkomna: inkomna, pausade: pausade, godkanda: godkanda, avslagna: avslagna },
			processData: true,
			cache: false,
			success:
				function (data) {
					if (!data.GetBlankettPosterTodoResult.HasError) {
						$("#" + selector + " .list-table tbody").html(
							$("#inkomna-blanketter-template").render(data.GetBlankettPosterTodoResult.Data)
						);
					}
					$(".blockable-container").unblock();
				},
			error:
				function (xhr) {
					$(".blockable-container").unblock();
					kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
				}
		});
	}

	return {
		getPersonList: getPersonList,
		getByggnadList: getByggnadList,
		getBlankettList: getBlankettList,
	}
}();